local M = {}

M.get_branch_name = function()
    if vim.g.divo_git_branch_done == 1 then
        return vim.g.divo_git_branch_name
    end
    --show detached HEAD
    --Thanks: https://stackoverflow.com/questions/6059336/how-to-find-the-current-git-branch-in-detached-head-state
	local branch = vim.fn.system("git rev-parse --abbrev-ref HEAD 2> /dev/null | tr -d '\n'")
    if branch == 'HEAD' then
        branch = vim.fn.system("git branch -a --contains HEAD | sed -n 1p | awk '{ printf $5 }' 2> /dev/null | tr -d '\n'")
        branch = string.sub(branch,1,4)
    end
    vim.g.divo_git_branch_done = 1
	if branch ~= "" then
        vim.g.divo_git_branch_name = branch
		return branch
	end

    vim.g.divo_git_branch_name = ''
	return ""
end

return M
-- EOP
